home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
CD ROM Paradise Collection 4
/
CD ROM Paradise Collection 4 1995 Nov.iso
/
science
/
sm32a.zip
/
SYMBMATH.H41
< prev
next >
Wrap
Text File
|
1993-11-07
|
7KB
|
195 lines
4.9 Arrays, Lists, Vectors and Matrices
You can construct arrays and lists of arbitrary length, and the
entries in the arrays and lists can be of any type of value whatsoever:
constants, expressions with undefined variables, or equations.
A vector and matrix can be represented by a list or array. In a
matrix, the number of elements in each row should be the same, e.g.
[[a11, a12], [a21, a22]].
4.9.1 Arrays
4.9.1.1 Entering Arrays
You can define an array of a by assigning its element value into
its index:
a[1]:=1
a[2]:=4
or you can define arrays another way, with the command:
do(a[x]:=f(x), x from xmin to xmax step dx)
e.g.
do(a[j] := 2*j, j from 1 to 2)
You can define 2-dimentional array by
a[1,1]:=11
a[1,2]:=12
a[2,1]:=21
a[2,2]:=22
or
do(do(a[j,k]:=j+k, j,jmin,jmax,dj), k,kmin,kmax,dk)
4.9.1.2 Accessing Arrays
After defining an array of a, you can access one of its element
by its index
IN: a[1]
OUT: 1
you also can list out all of its elements by
list(a[j], j,1,2,1)
e.g.
IN: do(a[j]:=2*j, j,1,2,1)
IN: list(a[j], j,1,2)
OUT: 1, 4
4.9.1.3 Modifying Arrays
You can modify an array by assigning new value into its index
IN: a[1]:=2
4.9.1.4 Operating Arrays
e.g.
after defining 2 arrays a and b, find their dot time, a .* b.
IN: a[1]:=1, a[2]:=2 # define array a
IN: b[1]:=11, b[2]:=12 # define array b
IN: p:=0
IN: do(p:=p + a[j]*b[j], j,1,2,1) # a .* b
4.9.2 Lists
4.9.2.1 Entering Lists
You can define a list by putting its elements between two square
brackets. e.g. [1,2,3]
You can define lists another way, with the command:
[ list(f(x), x from xmin to xmax step dx) ]
This is similar to the sum command, but the result is a list:
[f(xmin), f(xmin+dx), ..., f(xmin+x*dx), ...]
which continues until the last value of xmin + x*dx <= xmax.
You also can assign the list to a variable, which variable name
become the list name:
a := [1,2,3] # define the list of a
b := [f(2), g(1), h(1)] # assumes f,g,h defined
c := [[1,2],3,[4,5]] # define the list of c
Lists are another kind of value in SymbMath, and they can be assigned
to variables just like simple values. (Since variables in SymbMath
language are untyped, you can assign any value to any variable.).
A function can have a list for its value:
f(x_) := [sqrt(x), -sqrt(x)]
e.g.
IN: squreroot(x_) := [sqrt(x), -sqrt(x)]
IN: squreroot(4)
OUT: [2, -2]
A function can have a list for its arguement:
abs([-1,2])
Try
a := [ list(j^2, j from 0 to 10 step 1) ]
f(x_) := [ list(x^j, j from 0 to 6 step 1) ]
b := f(-2)
4.9.2.2 Accessing Lists
You can find the value of the j-th member in a list by
member([a,b], j)
The first member of a list is always member(x, 1).
If you have assigned a list to a variable x, you can access the
j-th element by the list index x[j]. The first element of x
is always x[1]. If the x[j] itself is a list, then its j-th element is
accessed by repeating the similar step. But you can not use the list
index unless the list is already assigned to x.
e.g.
IN: x := [[1,2],3,[4,5]] # define the x list
IN: x[1], x[2] # take its first and 2nd element
OUT: [1, 2], 3
IN: x # access the entire list of x
OUT: [[1, 2], 3, [4,5]]
IN: member(x, 2) # same as x[2]
OUT: 3
An entire sub-list of a list x can be accessed with the
command x[j], which is the list:
[x[j], x[j+1], ... ]
4.9.2.3 Modifying Lists
The subs() replaces the value of the element in
the list, as in the variables. e.g.
IN: subs([a,b,c], a = a0)
OUT: [a0, b, c]
Note that you cannot modify lists by assignment.
4.9.2.4 Operating Lists
Lists can be added, subtracted, multiplied, and divided by
other lists or by constants. When two lists are combined, they are
combined term-by-term, and the combination stops when the shortest
list is exhausted. When a scalar is combined with a list, it is
combined with each element of the list. Try:
a := [1,2,3]
b := [4,5,6]
a + b
a / b
3 * a
b - 4
Example 4.9.2.4.1.
Two lists are added.
IN: [a1,a2,a3] + [b1,b2,b3]
OUT: [a1 + b1, a2 + b2, a3 + b3]
IN: last[1]
OUT: a1 + b1
If L is a list, then f(L) results in a list of the values,
even though f() is the differentiation or integration function (d() or
inte()).
IN: sqrt([a, b, c])
OUT: [sqrt(a), sqrt(b), sqrt(c)]
IN: d([x, x^2, x^3], x)
OUT: [1, 2*x, 3*x^2]
If you use a list as the value of a variable in a function,
SymbMath will try to use the list in the calculation.
You can sum all the elements in a list x by
listsum(x)
Example:
IN: listsum([a,b,c]^2)
OUT: a^2 + b^2 + c^2
This function takes the sum of the squares of all the elements
in the list x.
You can do other statistical operations (see Section 4.10.
Statistics) on the list, or plot the list of numeric data (see
Section 5. Plot).
You can find the length of a list (the number of elements in a
list) with:
length(a)
4.9.3 Vectors and Matrices
You can uses arrays or lists to represent vectors, and lists of
lists to represent matrices.
Vectors and matrices can be operated by "+" and "-" with vectors
and matrixes, by "*" and "/" with a scalar, and by diff() and inte().
These operations are on each element, as in lists.
You can use lists as vectors, adding them and multiplying them
by scalars. For example, the dot product of two vectors of a and b is:
sum(a[j]*b[j], j from 1 to jmax)
You can even make this into a function:
dottime(x_, y_) := listsum(x*y)
e.g.
represnt the dot product of two vectors by arrays
IN: a[1]:=1, a[2]:=2 # define array a
IN: b[1]:=11, b[2]:=12 # define array b
IN: p:=0
IN: do(p:=p + a[j]*b[j], j,1,2,1) # a .* b
represnt the dot product of two vectors by lists
IN: dottime([1,2], [11,12]) # by lists in function dottime()
How about the cross product:
cross(a,b) = [a[2]*b[3]-b[2]*a[3],a[3]*b[1]-b[3]*a[1],a[1]*b[2]-b[1]*a[2]]